<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\CardUrlLinks;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class WebsiteController extends Controller
{
    /**
     * @var User
     */
    private $user = null;

    public function __construct()
    {
        $this->user = Auth::user();
    }
    /**
     * 网址列表
     * @param CardUrlLinks $cardUrlLinks
     * @return \Illuminate\Http\Response
     */
    public function index( CardUrlLinks $cardUrlLinks)
    {

        $sites = $cardUrlLinks->where('user_id', '=', $this->user->id)->orderByDesc('created_at')->get();
        $data = [];
        foreach ($sites as $site){
            $data[] = [
                'id' => $site->id,
                'title' => $site->title,
                'url' => $site->url,
                'created_at' => $site->created_at->format('Y-m-d')
            ];
        }
        return $this->renderJson($data);
    }

    /**
     * 单个链接信息
     * @param $id
     * @param CardUrlLinks $cardUrlLinks
     * @return \Illuminate\Http\Response
     */
    public function show($id, CardUrlLinks $cardUrlLinks)
    {
        $site = $cardUrlLinks->findOrFail($id);
        $data = [
            'id' => $site->id,
            'title' => $site->title,
            'url' => $site->url,
            'logo' => $site->logo_picture_path
        ];
        return $this->renderJson($data);
    }

    /**
     * 添加链接
     * @param Request $request
     * @param CardUrlLinks $cardUrlLinks
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, CardUrlLinks $cardUrlLinks)
    {
        $this->validate($request, [
            'title' => 'required',
            'url' => 'required',
            'logo' => 'required'
        ], [
            'required' => ':attribute不能为空'
        ], [
            'title' => '标题',
            'url' => '链接',
            'logo' => 'logo'
        ]);


        if ($cardUrlLinks->add($request)){
            return $this->success();
        }
        return $this->error('添加失败');
    }

    /**
     * 更新网址
     * @param Request $request
     * @param $id
     * @param CardUrlLinks $cardUrlLinks
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id, CardUrlLinks $cardUrlLinks)
    {
        $site = $cardUrlLinks->findOrFail($id);
        $this->validate($request, [
            'title' => 'required',
            'url' => 'required',
            'logo' => 'required'
        ], [
            'required' => ':attribute不能为空'
        ], [
            'title' => '标题',
            'url' => '链接',
            'logo' => 'logo'
        ]);


        if ($site->add($request)){
            return $this->success();
        }
        return $this->error('添加失败');
    }

    public function destroy($id, CardUrlLinks $cardUrlLinks)
    {
        $cardVideo = $cardUrlLinks->findOrFail($id);
        try {
            $res = $cardVideo->delete();
            if ($res){
                return $this->success();
            }
        } catch (\Exception $e) {
        }
        return $this->error('删除失败');
    }
}
